package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @Classname ModifyPreModeAttrHandler
 * @Description 提前终止预付期限新增修改模式
 * @Author chen.cheng110
 * @Date 2021-09-17 16:02
 */
@Service
public class ModifyPreModeAttrHandler extends AbstractAttrHandler {
    @Autowired
    private IQuerySceneInstService querySceneInstService;
    @Autowired
    private IOperateSceneCommonService operateSceneCommonService;

    @Override
    public void init(CommonAttrVO commonAttr) {
        String sceneInstId = KiteWebContext.getContext().getCurrentSceneInstId();
        // 修改模式
        KiteAttr modifyModeAttr = querySceneInstService.getCommonAttr(sceneInstId, "ORD10281");
        if (Objects.nonNull(modifyModeAttr)) {
            // 是0 否1 是否修改预付续约模式如果选“是”，“修改模式”展示且必填；反之则不展示
            if (KiteStringUtils.equals("0", commonAttr.getValue())) {
                modifyModeAttr.setIsNull(KeyConsts.IFTRUE_F);
                modifyModeAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
            } else if (KiteStringUtils.equals("1", commonAttr.getValue())) {
                modifyModeAttr.setIsNull(KeyConsts.IFTRUE_T);
                modifyModeAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
            }
            operateSceneCommonService.setCommonAttr(sceneInstId, modifyModeAttr);
        }
    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        // 修改模式
        KiteAttr modifyModeAttr = querySceneInstService.getCommonAttr(sceneInstId, "ORD10281");
        if (Objects.nonNull(modifyModeAttr)) {
            // 是0 否1 是否修改预付续约模式如果选“是”，“修改模式”展示且必填；反之则不展示
            if (KiteStringUtils.equals("0", value)) {
                modifyModeAttr.setIsNull(KeyConsts.IFTRUE_F);
                modifyModeAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
            } else if (KiteStringUtils.equals("1", value)) {
                modifyModeAttr.setIsNull(KeyConsts.IFTRUE_T);
                modifyModeAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
            }
            operateSceneCommonService.setCommonAttr(sceneInstId, modifyModeAttr);
        }
        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }
}
